﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ASPNET.StarterKit.BusinessLogicLayer;
using ASPNET.StarterKit.DataAccessLayer;
using System.Data.SqlClient;


public partial class InventoryTracker_GoodsReceipt : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        btnGRNew.Enabled = true;
        btnGRDelete.Enabled = false;
        btnGREdit.Enabled = false;
        btnGRSubmit.Enabled = true;
        btnGRUpdate.Enabled = false;


    }
    protected void btnGRSubmit_Click(object sender, EventArgs e)
    {
        DataAccess DA = DataAccessHelper.GetDataAccess();
        GoodsReceipt aGoodsReceipt = new GoodsReceipt(int.Parse( ddrSupID.SelectedValue ),int.Parse( ddrItemCode.SelectedValue), int.Parse(txtQtyAccepted.Text), int.Parse(txtQtyRejected.Text), int.Parse(txtQtyPending.Text), float.Parse(txtBuyingRate.Text),int.Parse( txtVendorItemCode.Text),int.Parse( txtPONumber.Text));

        try
        {
            if (DA.CreateGoodsReceipt(aGoodsReceipt))
            {
                lblResult.Text = "Success";
            }
            else
            {
                lblResult.Text = "Failure";
            }
        }
        catch (Exception ex)
        {
            lblResult.Text = ex.Message.ToString();
        }


    }
    protected void DDlGoodsRec_SelectedIndexChanged(object sender, EventArgs e)
    {

        String SelectValue = DdlGoodsRec.SelectedValue;

        ddrSupID.Visible = false;
        ddrItemCode.Visible = false;
        txtSupplID.Visible = true;
        txtItemCodeI.Visible = true;

        btnGRNew.Enabled = true;
        btnGRDelete.Enabled = true;
        btnGREdit.Enabled = true;
        btnGRSubmit.Enabled = false;
        btnGRUpdate.Enabled = false;

        string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        String sql = "SELECT * FROM GoodsReceipt WHERE  [GRId]=" + SelectValue;

        SqlCommand cmd = new SqlCommand(sql, con);
        DataTable dataTable = GetDataTable(cmd);
        txtQtyAccepted.Text = dataTable.Rows[0]["QtyAccepted"].ToString();
        txtQtyRejected.Text = dataTable.Rows[0]["QtyRejected"].ToString();
        txtQtyPending.Text = dataTable.Rows[0]["QtyPending"].ToString();
        txtBuyingRate.Text = dataTable.Rows[0]["BuyingRate"].ToString();
        txtVendorItemCode.Text = dataTable.Rows[0]["VendorItemCode"].ToString();
        txtPONumber.Text = dataTable.Rows[0]["PONUmber"].ToString();
        
        String sqlSupp = "SELECT * FROM SupplierMaster WHERE SupID=" + dataTable.Rows[0]["SupID"];
        SqlCommand cmdloc = new SqlCommand(sqlSupp, con);
        DataTable dataTableloc = GetDataTable(cmdloc);
        txtSupplID.Text = dataTableloc.Rows[0]["SupName"].ToString();

        String sqluom = "SELECT * FROM MaterialMaster WHERE ItemCode=" + dataTable.Rows[0]["ItemCode"];
        SqlCommand cmduom = new SqlCommand(sqluom, con);
        DataTable dataTableuom = GetDataTable(cmduom);
        txtItemCodeI.Text = dataTableuom.Rows[0]["ItemName"].ToString();

        con.Close();

    }

    public static DataTable GetDataTable(SqlCommand cmd)
    {

        SqlDataAdapter da = new SqlDataAdapter();
        //set the SelectCommand of the adapter
        da.SelectCommand = cmd;
        // create a new DataTable
        DataTable dtGet = new DataTable();
        //fill the DataTable
        da.Fill(dtGet);
        //return the DataTable
        return dtGet;

    }

    
    
    
    
    protected void btnGRNew_Click(object sender, EventArgs e)
    {
        ddrSupID.Visible= true;
        ddrItemCode.Visible= true;
        txtSupplID.Visible = false;
        txtItemCodeI.Visible = false;

        btnGRNew.Enabled = false;
        btnGRDelete.Enabled = false;
        btnGREdit.Enabled = false;
        btnGRSubmit.Enabled = true;
        btnGRUpdate.Enabled = false;

        txtQtyAccepted.Text = "";
        txtQtyRejected.Text = "";
        txtQtyPending.Text = "";
        txtBuyingRate.Text = "";
        txtVendorItemCode.Text = "";
        txtPONumber.Text = "";
        ddrSupID.SelectedIndex = 0;
        ddrItemCode.SelectedIndex = 0;
    }
    protected void btnGRDelete_Click(object sender, EventArgs e)
    {
        String SelectValue = DdlGoodsRec.SelectedValue;
        btnGRNew.Enabled = false;
        btnGRDelete.Enabled = true;
        btnGREdit.Enabled = false;
        btnGRSubmit.Enabled = false;
        btnGRUpdate.Enabled = false;


        string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        String sql = "DELETE FROM GoodsReceipt WHERE  [GRId]=" + SelectValue;
        SqlCommand cmd = new SqlCommand(sql, con);
        cmd.ExecuteNonQuery();
        con.Close();

        txtQtyAccepted.Text = "";
        txtQtyRejected.Text = "";
        txtQtyPending.Text = "";
        txtBuyingRate.Text = "";
        txtVendorItemCode.Text = "";
        txtPONumber.Text = "";
        ddrSupID.SelectedIndex = 0;
        ddrItemCode.SelectedIndex = 0;
    }
    protected void btnGREdit_Click(object sender, EventArgs e)
    {
        ddrSupID.Visible = true;
        ddrItemCode.Visible = true;
        txtSupplID.Visible = false;
        txtItemCodeI.Visible = false;

        btnGRNew.Enabled = false;
        btnGRDelete.Enabled = false;
        btnGREdit.Enabled = false;
        btnGRSubmit.Enabled = false;
        btnGRUpdate.Enabled = true;


       
    }
    protected void btnGRUpdate_Click(object sender, EventArgs e)
    {
        btnGRNew.Enabled = false;
        btnGRDelete.Enabled = false;
        btnGREdit.Enabled = false;
        btnGRSubmit.Enabled = false;
        btnGRUpdate.Enabled = true;


        String SelectValue = DdlGoodsRec.SelectedValue;

        string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        String sql = "UPDATE GoodsReceipt SET  QtyAccepted = '" + txtQtyAccepted.Text + "' , QtyRejected = '" + txtQtyRejected.Text + "' , QtyPending ='" + txtQtyPending.Text + "', BuyingRate ='" + txtBuyingRate.Text + "' , VendorItemCode ='" + txtVendorItemCode.Text + "' , PONumber ='" + txtPONumber.Text + "' WHERE GRId=" + SelectValue;
        SqlCommand cmd = new SqlCommand(sql, con);
        cmd.ExecuteNonQuery();
        con.Close();
    }
}